<!--
/*#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This code is not supported by Google
#
*/
-->
<!--
/*
Sample .aspx code for Google Commerce Search to submit ShoppingAPI-Search queries 
 Usage:
  
  Edit the file and specify the GCS instance ID (cx) and the API key you're 
  using.
Deploy on any IIS server with .NET 4+ and enter in your GCS cx number, API key
https://code.google.com/apis/console/
http://www.google.com/cse/commercesearch/manage
*/

/**
 * Sample code to issue a simple GCS query using the Beta Google Client Libraries:
 * http://code.google.com/p/google-api-dotnet-client/
 * This code is NOT supported by Google Enterprise.   
 *  <Reference Include=Google.Apis: Google.Apis.dll
 *  <Reference Include=Google.Apis.Shopping.v1=Google.Apis.Shopping.v1.dll</HintPath>
 *  <Reference Include=Newtonsoft.Json.Net35=Newtonsoft.Json.Net35.dll
 *  (included in the library and samples on the .NET client above)    
 */
-->

<%@ Page Language="c#"  AutoEventWireup="true" Async="true" AsyncTimeout="5"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="Google.Apis.Shopping.v1" %>
<%@ Import Namespace="Google.Apis.Shopping.v1.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"> 
    
    String devkey = "YOUR_KEY";
    String cx = "YOUR_CX";
    String country = "us";
    String language = "en";
    String totalResults = "";
    String html_attributes = "";
    String html_results = "";
    void Page_Load(object sender, EventArgs e)
    {
        string q = Request.QueryString["q"];
        
        ShoppingService service = new ShoppingService();
        service.Key = devkey;
        var request = service.Products.List("cx:" + cx);
        request.Q = q;
        request.Country = "us";
        request.Language = "en";
        request.StartIndex = 1;
        request.MaxResults = 10;
        request.Safe = false;
        request.SpellingEnabled = true;
        request.SpellingUseGcsConfig = true;
        request.PromotionsEnabled = true;
        request.PromotionsUseGcsConfig = true;
        request.RedirectsEnabled = true;
        request.RelatedQueriesUseGcsConfig = true;
        request.FacetsEnabled = true;
        request.FacetsUseGcsConfig = true;
        request.FacetsDiscover = "10:10";
        request.Availability = "outOfStock,limited,inStock";
        //request.MaxVariants = 5;
        request.FacetsUseGcsConfig = true;
        Products response = request.Fetch();
        html_results = html_results + "<table>";
        if (response.Items != null)
        {
            totalResults = response.TotalItems.ToString();
            foreach (Product item in response.Items)
            {
                html_results = html_results + "<tr><td><strong><a href='" + item.ProductValue.Link + "'>" + item.ProductValue.Title + "</a></strong><br/>";
                if (item.ProductValue.Images != null)
                    foreach (ShoppingModelProductJsonV1.ImagesData i in item.ProductValue.Images)
                    {
                        html_results = html_results + "<img src ='" + i.Link + "'/><br/>";
                    }
                if (item.ProductValue.Inventories != null)
                    foreach (ShoppingModelProductJsonV1.InventoriesData i in item.ProductValue.Inventories)
                    {
                        html_results = html_results + "&nbsp;&nbsp;&nbsp;&nbsp;" + i.Currency + " " + i.Price + "<br/>";
                    }
                if (item.ProductValue.Attributes != null)
                    foreach (ShoppingModelProductJsonV1.AttributesData a in item.ProductValue.Attributes)
                    {
                        html_results = html_results + "&nbsp;&nbsp;&nbsp;&nbsp;" + a.DisplayName + " " + a.Value + "<br/>";
                    }
                if (item.ProductValue.Variants != null)
                {
                    html_results = html_results + "<em>Variants:</em><table>";
                    foreach (ShoppingModelProductJsonV1.VariantsData v in item.ProductValue.Variants)
                    {
                        html_results = html_results + "<tr><td>" + v.Variant.Title + "</td><td>" + v.Variant.ProvidedId + "</td></tr>";
                    }
                    html_results = html_results + "</table>";
                }
                html_results = html_results + "</td></tr>";
            }
            foreach (Products.FacetsData f in response.Facets)
            {
                html_attributes = html_attributes + "<tr><td><strong>" + f.DisplayName + "</strong><br/>";
                foreach (Products.FacetsData.BucketsData b in f.Buckets)
                { 
                    html_attributes = html_attributes + "&nbsp;&nbsp;&nbsp;&nbsp;" + b.Value + "(" + b.Count + ")" + "<br/>";
                }
                html_attributes = html_attributes + "</td></tr>";
            }
        }
                    
        
    }   
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="https://developers.google.com/css/screen.css" />
    <link href='//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>   
</head>
<body class="two-column docs">
    <div id="gc-wrapper">
    <div id="gc-main">
    <div id="gc-content">
    
    <center>
   <img src="http://www.google.com/intl/en/images/logos/commerce_search_logo.gif" alt="Google Commerce Search"/>
        <form id="searchForm" method="GET">
       <table>
       <tr><td>search:</td><td><input id="q" name="q" type="text" size="45%" autocomplete="off" /></td></tr>     
   </table>	   	   
   <input type="submit" value="Submit" "/>	
        </form>	
    </center>
    
    <br/>
    <td>Total Results:  <%=totalResults%></td>
    <hr/>
<table width="100%" border=2>
<tr align="top" >
 <td width="25%" id="attributes" name="attributes"  valign="top"><b><u>Attributes</u></b><table><%=html_attributes%></table></td>
 <td width="75%" id="results" name="results"  valign="top" ><b><u>Results</u></b><table><%=html_results%></table></td>
</tr>
</table>
    </div>
    </div>
    </div>
</html>
